/*************************************************
Author: will
Date: 10/16/2012
Description:
Notes:
*************************************************/

#ifndef __CStorageMgr_h__
#define __CStorageMgr_h__

#include "CUserStorage.h"
#include "CTArray.h"
#include "commclass.h"
#include "../equip/CUserEquip.h"
#include "../item/CItemMgr.h"

class PBMsgHead;
class MsgAnsUserStorageInfo;
class MsgGridItemInfo;
class CDropItemData;
class CPlayerOnlineData;
class CPlayerDetailInfoEx1;
class MsgItemSynInfo;
class MsgReqActStorage;
class MsgAnsActStorage;
class MsgScrollDetailInfo;
class MsgAnsEquipMakeInfo;
class MsgAnsItemDetailInfos;

class CStorageMgr
{
public:
    typedef CTLib::CTArray<CStorageGrid, MAX_BAG_GRID_COUNT + MAX_WAREHOUSE_GRID_COUNT> T_STORAGE_GRIDS;

	CStorageMgr()
    {
	}

	virtual ~CStorageMgr()
    {
	}

    int InitData(CUserStorage& stUserStorage, StorageData& stData);
    int GetData(CUserStorage& stUserStorage, StorageData& stData);

    int GetBagList(CUserStorage& stUserStorage, T_STORAGE_GRIDS& astStorageGrid);
    int GetWareHouseList(CUserStorage& stUserStorage, T_STORAGE_GRIDS& astStorageGrid);

    int GetGridInfoByPos(CUserStorage& stUserStorage, bool bIsInBag, unsigned short usPos, CStorageGrid& stGrid);
    
    //根据等级更新格子数
    void UpdateGridCount (CUserStorage& stUserStorage, short sRoleLevel, bool & bNewUnlocked);

    //根据等级获取格子数
    int GetGridCount (short sRoleLevel, unsigned char ucStorageType, unsigned char & ucGridCount);

    //获取激活格子的金币价格
    int GetUnLockGridPrice (CUserStorage& stUserStorage, unsigned char ucStorageType, unsigned short & usGoldPrice);
    int UnLockGrid (CUserStorage& stUserStorage, unsigned char ucStorageType);

    //取出物品,外部仅支持,取出背包的物品.
    int TakeOutGrid(CUserStorage& stUserStorage, unsigned short usPos, CStorageGrid& stGrid);

    //消耗物品
    int TakeOutItem(CUserStorage& stUserStorage, unsigned char ucItemType, unsigned int uiItemID, int iCount);

    //指定格子上消耗物品
    int TakeOutItem(CUserStorage& stUserStorage, unsigned short usBagPos, short shCount);

	//替换背包物品.    
	int ReplaceGrid(CUserStorage& stUserStorage, bool bIsInBag, unsigned short usPos, CStorageGrid & stNewGrid);

    //尽可能地添加物品到背包, 如果背包满了的话, 返回值为0, shCount保存剩下的物品数.
    int TryAddItem(CUserStorage& stUserStorage, unsigned char ucItemType, unsigned int uiItemID, short& shCount);

    //叠加到背包中, 要么全加入或者全不加入.
    int AddItem( CUserStorage& stUserStorage, unsigned char ucItemType, unsigned int uiItemID, short shCount);

    //获取物品数量
    int GetItemCount(CUserStorage& stUserStorage, unsigned char ucItemType, unsigned int uiItemID);

    //重新设置物品的有效时间
    int ClearTime(CUserStorage& stUserStorage, unsigned short usBagPos, int iNewTime);

    //排序
    int SortStorage(CUserStorage& stUserStorage, bool bIsSortBag);

    //合并背包
    int MergeToWareHouse(CUserStorage& stUserStorage);

    //获取背包信息推送协议内容
    void GetStorageInfo(unsigned char ucBagType, unsigned int uiGID, CUserStorage& stUserStorage, MsgAnsUserStorageInfo& stAns);
	void GetGridInfo(unsigned int uiGid, CStorageGrid & stStorageGrid, MsgGridItemInfo & stGridItemInfo);	
    void GetEquipGridInfo (unsigned int uiGid, CStorageGrid & stStorageGrid, MsgGridItemInfo & stGridItem);
    int GetScrollDetailInfo (CPlayerOnlineData & stPlayerOnlineData, unsigned short usScrollId, MsgScrollDetailInfo & stScrollInfo);    
    void GetScrollSynInfo(CUserStorage &stUserStorage, CScrollMaterial & stNeedMaterial, MsgItemSynInfo & stSynItemInfo);

    //向前台推送卷轴信息
    void SendScrollDetailInfoToFront(CPlayerOnlineData& stPlayerOnlineData, unsigned short usScrollId, PBMsgHead stMsgHead, CTLib::CEnpNetHead& stEnpHead);
    void SendInfoToFront(unsigned char ucBagType, unsigned int uiGID, CUserStorage& stUserStorage, PBMsgHead stMsgHead, CEnpNetHead& stEnpHead);

	//制造物品, 装备合成需要卷轴. 魔药合成可使用金币,不需要卷轴.
	int MakeItem(CUserStorage& stUserStorage, CStorageGrid & stScrollGrid, unsigned char ucOpType, bool bScrollNecessary);
    bool IsScrollMaterialEnough(CUserStorage &stUserStorage, unsigned short usScrollConfigId);
    int GetScrollGoldCost(CUserStorage &stUserStorage, unsigned short usScrollConfigId, int &iGoldCost);

    //魔晶操作:　合成
    int CombineCrystal (CUserStorage& stUserStorage, unsigned char ucOldCrystalId, unsigned char ucCombinedId);

    //魔晶操作:　一键合成
    int OneKeyCrystal (CPlayerDetailInfo & stPlayerDetailInfo, CUserStorage& stUserStorage, unsigned char ucCombinedLevel);

    //魔晶操作:　魔晶分解
    int DecomposeCrystal (CPlayerBaseInfo & stPlayerBaseInfo, CPlayerOnlineData & stPlayerOnlineData, unsigned short usBagPos, CStorageGrid & stResult);

    //魔晶操作:　碎片合成
    int ComposeCrystalFrag (CUserStorage& stUserStorage, unsigned short usBagPos, CStorageGrid & stResult);

    //打开礼包
    int OpenGiftInBag (CUserStorage& stUserStorage, unsigned short usBagPos, unsigned int uiGid, CDropItem & stDropItem);
    int OneKeyOpenGiftInBag (CUserStorage& stUserStorage, unsigned short usBagPos, unsigned int uiGid, CDropItem & stDropItem, int & iOptRet);
    int OpenGift (CUserStorage& stUserStorage, unsigned int uiGiftId, unsigned int uiGid, CDropItem & stDropItem);

    //预览礼包中物品
    int PreViewGiftInfo (CUserStorage& stUserStorage, unsigned int uiGiftId, unsigned int uiGid,  CDropItem & stGiftItem);

    //是否有礼包可用
    int CheckGiftAvail (CUserStorage& stUserStorage, unsigned int uiGid);

    void SendActStorageToFront(CPlayerDetailInfoEx1& stPlayerDetailEx1, int iModuleID);

    int AddOneItemToActStorage(CPlayerDetailInfoEx1& stPlayerDetailEx1, CDropItemData& stDropItemData, int iModuleID);

    int AddItemToActStorage(CPlayerDetailInfoEx1& stPlayerDetailEx1, CDropItem& stDropItem, int iModuleID);

    int GetActStorageItem(CPlayerDetailInfoEx1& stPlayerDetailEx1,
            CPlayerOnlineData& stPlayerOnline, MsgReqActStorage& stReq,
            MsgAnsActStorage& stAns);
};

#endif // __CStorageMgr_h__

